package com.beilang.hotline.dept.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.dept.model.DeptModel;
import com.beilang.hotline.dept.to.SearchTO;


public class DeptDAO  extends PaginationBaseDAO {
	public DeptModel createDept(DeptModel deptModel) {
        deptModel.setDeleteInd("N");
        return (DeptModel)this.save(deptModel);
	}

	public DeptModel saveDept(DeptModel deptModel) {
		return (DeptModel)this.save(deptModel);
	}

	public void deleteDept(Long deptId){
        DeptModel dept= this.getDept(deptId);
        dept.setDeleteInd("Y");
        this.save(dept);
	}

	public DeptModel getDept(Long deptId){
		return (DeptModel)this.get(DeptModel.class, deptId);
	}

	public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();
        String deptName=StringUtil.convertEmpty2Null(searchTO.getDeptName());
        if(deptName!=null)deptName="%"+deptName+"%";
        map.put("deptName", deptName);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchDeptCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        String deptName=StringUtil.convertEmpty2Null(searchTO.getDeptName());
        if(deptName!=null)deptName="%"+deptName+"%";
        map.put("deptName", deptName);
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchDeptList", map);
    }

    protected String mapSortField(String dataField){
        //return "record_id";
        if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("deptName")) return "dept_name";
        return dataField;
    }
}
